import Store from "../core/store.js";
import { mergeOptions, convertColorFormat, jsonToVec3 } from "../core/common.js";
let store = new Store();

let ExtrudeGeometry = {
  add(options, cb) {
    let defaults = {
      type: "entity",
      points: null,
      style: {
        imgUrl: "",
        roofMap: "",
        wallMap: "",
        color: "#00ffff",
        roofColor: "",
        lineColor: "#00ffff",
        linewidth: 2.0,
        opacity: 0.99,
        offsetX: 0,
        offsetY: 0,
        scale: 1,
        castShadow: false,
        waterShader: false,
        perPositionHeight: true,
        height: 100,
        baseHeight: 0,
        gradient: true,
        animation: null,
        objectName: "",
        tag: "",
      },
    };
    options = mergeOptions(defaults, options);
    options.points = jsonToVec3(options.points);

    let instance = null;
    if (options.type == "outline") {
      instance = this.drawBuildingOutline(options.points, options.style);
    } else {
      instance = this.drawExtrudeEntity(options.points, options.style);
    }

    let id = store.add(instance, options.id);

    cb && cb(id);
    return id;
  },
  drawBuildingOutline(points, style) {
    let outer = new SSmap.Cartesian3Vector();
    points.forEach((point) => {
      if (style.baseHeight) {
        point = point.toCartographic();
        point.height = style.baseHeight;
      }
      outer.push_back(point.toCartesian3());
    });

    let extrudeEntity = new SSmap.BuildingOutline();
    extrudeEntity.setOuter(outer);
    extrudeEntity.extrudeHeight = style.height;
    extrudeEntity.altitude = style.baseHeight;
    extrudeEntity.url = style.imgUrl;

    //着色
    let color = convertColorFormat(style.color);
    extrudeEntity.color = SSmap.Color.fromRgb(
      color.r,
      color.g,
      color.b,
      color.a
    );
    let lineColor = convertColorFormat(style.lineColor);
    extrudeEntity.strokeColor = SSmap.Color.fromRgb(
      lineColor.r,
      lineColor.g,
      lineColor.b,
      lineColor.a
    );
    extrudeEntity.gradient = style.gradient;
    extrudeEntity.strokeWidth = style.linewidth;
    extrudeEntity.name = style.objectName;
    extrudeEntity.alpha = style.opacity;
    extrudeEntity.perPositionHeight = style.perPositionHeight;

    //动画
    let animation = style.animation;
    if (animation) {
      extrudeEntity.animationRun = true;
      extrudeEntity.animationType = SSmap.AnimationType[animation.type];
      extrudeEntity.animationTimer = animation.duration || 2000;
      extrudeEntity.cw = animation.cw;
    }

    extrudeEntity.create();
    extrudeEntity.parent = SSmap.Entity.root();
    outer.delete();

    return extrudeEntity;
  },
  drawExtrudeEntity(points, style) {
    let scene = GlobalViewer.scene;
    //水平cart3集合
    let outer = new SSmap.Cartesian3Vector();
    points.forEach((point) => {
      outer.push_back(point.toCartesian3());
    });

    //拉伸体对象
    let extrudeEntity = new SSmap.ExtrudeEntity();
    extrudeEntity.setOuter(outer);
    extrudeEntity.extrudeHeight = style.height;
    extrudeEntity.altitude = style.baseHeight;

    //着色
    let color = convertColorFormat(style.color);
    extrudeEntity.color = SSmap.Color.fromRgb(
      color.r,
      color.g,
      color.b,
      color.a
    );

    color = convertColorFormat(style.roofColor || style.color);
    extrudeEntity.roofColor = SSmap.Color.fromRgb(
      color.r,
      color.g,
      color.b,
      color.a
    );

    extrudeEntity.waterShader = style.waterShader;

    //纹理
    style.wallMap && (extrudeEntity.wallMap = style.wallMap);
    style.roofMap && (extrudeEntity.roofMap = style.roofMap);

    extrudeEntity.create();

    let entity = extrudeEntity.createEntity();
    entity.travalRenderers(function (renderer) {
      if (style.castShadow) {
        renderer.castShadow = true;
      }
      renderer.material.opacity = style.opacity;
    });
    entity.objectName = style.objectName;
    entity.tag = style.tag;

    //加入场景
    scene.addEntity(entity);
    outer.delete();
    extrudeEntity.delete();

    return entity;
  },
  remove(id) {
    store.has(id) && store.remove(id);
  },
};

export default ExtrudeGeometry;
